package com.cd2cd.service.impl;

import com.cd2cd.comm.ServiceCode;
import com.cd2cd.domain.SysDict;
import com.cd2cd.domain.SysUser;
import com.cd2cd.domain.gen.SysDictCriteria;
import com.cd2cd.mapper.SysDictMapper;
import com.cd2cd.service.SysDictService;
import com.cd2cd.util.BeanUtil;
import com.cd2cd.util.StringUtil;
import com.cd2cd.vo.BaseRes;
import com.cd2cd.vo.DataPageWrapper;
import com.cd2cd.vo.SysDictVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class SysDictServiceImpl implements SysDictService {


    @Resource
    private SysDictMapper sysDictMapper;

    @Override
    public BaseRes<DataPageWrapper<SysDictVo>> list(Integer currPage, Integer pageSize, SysDictVo sysDictVo) {

        BaseRes<DataPageWrapper<SysDictVo>> res = new BaseRes<>();
        res.setData(new DataPageWrapper<SysDictVo>());

        int mysqlLength = pageSize;
        int mysqlOffset = (currPage - 1) * mysqlLength;

        SysDictCriteria sysDictCriteria = new SysDictCriteria();

        sysDictCriteria.setMysqlLength(mysqlLength);
        sysDictCriteria.setMysqlOffset(mysqlOffset);

        SysDictCriteria.Criteria criteria = sysDictCriteria.createCriteria();

        criteria.andDelFlagEqualTo(0);

        String name = sysDictVo.getName();
        if (StringUtil.isNotBlank(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        String status = sysDictVo.getStatus();
        if (StringUtil.isNotBlank(status)) {
            criteria.andStatusEqualTo(status);
        }

        long count = sysDictMapper.countByExample(sysDictCriteria);

        res.getData().setTotalCount(count);

        if (count > 0) {
            List<SysDict> sysDicts = sysDictMapper.selectByExample(sysDictCriteria);
            List<SysDictVo> rows = BeanUtil.voConvertList(sysDicts, SysDictVo.class);
            res.getData().setRows(rows);
        }

        return res;
    }

    @Override
    public BaseRes<SysDictVo> detail(Integer id) {

        BaseRes<SysDictVo> res = new BaseRes<>(ServiceCode.SUCCESS);

        SysDict sysDict = sysDictMapper.selectByPrimaryKey(id);

        if (sysDict != null) {
            SysDictVo sysDictVo = BeanUtil.voConvert(sysDict, SysDictVo.class);
            res.setData(sysDictVo);
        }

        return res;
    }

    @Override
    public BaseRes<String> del(Integer id) {

        SysDict sysDict = new SysDict();

        sysDict.setId(id);
        sysDict.setDelFlag(1);

        sysDictMapper.updateByPrimaryKeySelective(sysDict);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> add(SysDictVo sysDictVo) {

        sysDictVo.setCreateTime(new Date());
        sysDictVo.setUpdateTime(new Date());

        sysDictMapper.insertSelective(sysDictVo);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> modify(SysDictVo sysDictVo) {
        sysDictVo.setUpdateTime(new Date());
        sysDictMapper.updateByPrimaryKeySelective(sysDictVo);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }
}
